<template>
  <div class="cart">
  <my-header outerTitle="商品详情"></my-header>
    <div class="empty-cart" v-if="cartList.length===0">
      <img src="" alt="" class="empty-cart-img">
      <div class="empty-cart-text">购物车还是空的</div>
      <div class="btn">去逛逛</div>
    </div>
    <div class="cart-container" v-else v-for="(item,index) in cartList" :key="index">
    <!-- 选中与否两个类名控制： icon-checkbox-marked-circ（选中）icon-checkbox-blank-circle-outline（未选中） -->
      <span 
      class="iconfont toogle"
      :class="item.selectState===true?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'"
      @click="toggle(index)"
      ></span>
      <div class="item-detail">
        <img :src="item.img" :alt="item.title">
        <div class="item-info">
          <h3>{{item.title}}</h3>
          <div class="pay">
            <div class="pay-price">￥{{item.price}}</div>
            <div class="edit-quantity">
              <p class="operate-btn iconfont icon-minus" @click="handleMinus(index)"></p>
              <p class="btn-input">{{item.num}}</p>
              <p class="operate-btn iconfont icon-plus" @click="handleAddNum(index)"></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="cart-footer">
      <div class="cart-footer-left">
      <span class="iconfont toogle"
      @click="handleSelectAll"
      :class="isSelectAll?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'"
      ></span><span>全选</span>
      </div>
      <div class="cart-footer-center"><span>合计：</span><span class="total-price">￥{{totalPrice}}</span></div>
      <div class="cart-footer-right"><span class="goto-pay">结算({{totalNum}})</span></div>
    </div>
    <!-- 提示框 -->
    <div class="tig" v-if="visibility">
     <!-- <confirm 
      show-input
      title="你确定要删除吗"
      @on-cancel="onCancel"
      @on-confirm="onConfirm"
      @on-show="onShow"
      @on-hide="onHide">
      </confirm> -->
    </div>
  </div>
</template>
<script>
import myHeader from '@/components/myHeader'
import { Confirm } from 'vux'
export default {
   components: {
    myHeader,
    Confirm
  },
  data () {
    return {
      cartList:[],
      visibility:false
    }
  },
  created () {
    this.cartList = JSON.parse(localStorage.getItem('cartList') || '[]')
  },
  methods: {
    // 切换商品的状态
    toggle (index) {
      this.cartList[index].selectState = !this.cartList[index].selectState
    },
    // 点击全选
    // 点击全选
    handleSelectAll () {
      // 如果计算属性 isSelectAll 为true，就把所有的数据项的selectState变为false
      // 如果计算属性 isSelectAll 为false，就把所有的数据项的selectState变为true
      let isAll = this.isSelectAll
      this.cartList.map(item => {
        item.selectState = !isAll
      })
    },
    // 点击加号
    handleAddNum (index) {
      this.cartList[index].num += 1
    },
    // 点击减号
    handleMinus (index) {
      // 判断当前数量是否为1，为1时弹出提示框
      if (this.cartList[index].num === 1) {
    //     this.visibility=true
    //     onConfirm () {
    //   // console.log('on confirm')
    //   // 点击确定时，将该商品删除
            this.cartList.splice(index, 1)
    // }   
    
      } else {
        // 否则，将该商品的数量-1
        this.cartList[index].num -= 1
      }
    },
    // // 提示框
    // onCancel () {
    //   console.log('on cancel')
    // },
    // onShow () {
    //       console.log('plugin show')
    //     },
    // onHide () {
    //       console.log('plugin hide')
    //     }
  },
  computed: {
    // 计算总价
    totalPrice () {
      let price = 0
      // 循环遍历cartList
      this.cartList.map(item => {
        if (item.selectState) {
          price += item.price * item.num
        }
      })
      return price
    },
    // 总数量计算
    totalNum () {
      let num = 0
      this.cartList.map(item => {
        num += item.num
      })
      return num
    },
    // 全选与非全选
    isSelectAll () {
      let isAll = true
      this.cartList.map(item => {
        // 如果有一个选项没有选中，则全选为false
        if (!item.selectState) {
          isAll = false
        }
      })
      return isAll
    }
  },
  // 关闭页面时，将修改的数据保存到本地存储中,将数据转换为json格式的字符串才能保存
  beforeDestroy () {
    localStorage.setItem('cartList', JSON.stringify(this.cartList))
  }
}
</script>
<style lang="scss" scoped>
.cart {
  height: 320px;
  margin-top: 45px;
  font-size: 14px;
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    .empty-cart-img {
      height: 90px;
      width: 90px;
    }
    .empty-cart-text {
      font-size: 20px;
      color: #999;
      padding: 15px 0;
    }
    .btn {
      font-size: 20px;
      padding: 15px 55px;
      text-align: center;
      margin: 0 auto;
      border-radius: 10px;
      background: #ed601b;
      color: #fff;
    }
  }
  .cart-container {
    display: flex;
    align-items: center;
    margin: 5px;
    padding: 8px 10px;
    border: 1px solid #eeeeee;
    border-radius: 20px;
    .toogle {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 20px;
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
  .item-detail {
    display: flex;
    flex: 1;
    img {
      margin: 2px 0 2px 5px;
      width: 90px;
      height: 90px;
    }
    .item-info {
      width: 100%;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      h3 {
        font-size: 16px;
        font-weight: 400;
      }
      .pay {
        display: flex;
        justify-content: space-between;
        .pay-price {
          color: #ff5500;
          font-weight: bold;
          line-height: 30px;
        }
        .edit-quantity {
          display: flex;
          height: 30px;
          line-height: 30px;
          .operate-btn {
            padding: 0 10px;
            font-size: 20px;
            color: #bababa;
          }
          .btn-minus {
            font-size: 14px;
          }
        }
      }
    }
  }
  .cart-footer {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    border-top: 1px solid #e7e7e7;
    background-color: #fff;
    .cart-footer-left {
      display: flex;
      justify-content: cneter;
      align-items: center;
      span {
        display: block;
        height: 50px;
        padding: 0 5px;
      }
    }
    .cart-footer-center {
      .total-price {
        color: #ff5500;
        font-weight: bold;
      }
    }
    .cart-footer-right {
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      background-color: #ff5500;
      .goto-pay {
        color: #fff;
      }
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
}
</style>
